<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>25.4. Working with Dates and Times</title>
<link rel="stylesheet" href="dbstyle.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.72.0">
<link rel="start" href="index.html" title="Programmer's Reference Guide">
<link rel="up" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="prev" href="zend.locale.parsing.html" title="25.3. Normalization and Localization">
<link rel="next" href="zend.locale.languages.html" title="25.5. Supported Languages for Locales">
<link rel="chapter" href="introduction.html" title="Chapter 1. Introduction to Zend Framework">
<link rel="chapter" href="zend.acl.html" title="Chapter 2. Zend_Acl">
<link rel="chapter" href="zend.auth.html" title="Chapter 3. Zend_Auth">
<link rel="chapter" href="zend.cache.html" title="Chapter 4. Zend_Cache">
<link rel="chapter" href="zend.config.html" title="Chapter 5. Zend_Config">
<link rel="chapter" href="zend.console.getopt.html" title="Chapter 6. Zend_Console_Getopt">
<link rel="chapter" href="zend.controller.html" title="Chapter 7. Zend_Controller">
<link rel="chapter" href="zend.currency.html" title="Chapter 8. Zend_Currency">
<link rel="chapter" href="zend.date.html" title="Chapter 9. Zend_Date">
<link rel="chapter" href="zend.db.html" title="Chapter 10. Zend_Db">
<link rel="chapter" href="zend.debug.html" title="Chapter 11. Zend_Debug">
<link rel="chapter" href="zend.dojo.html" title="Chapter 12. Zend_Dojo">
<link rel="chapter" href="zend.dom.html" title="Chapter 13. Zend_Dom">
<link rel="chapter" href="zend.exception.html" title="Chapter 14. Zend_Exception">
<link rel="chapter" href="zend.feed.html" title="Chapter 15. Zend_Feed">
<link rel="chapter" href="zend.filter.html" title="Chapter 16. Zend_Filter">
<link rel="chapter" href="zend.form.html" title="Chapter 17. Zend_Form">
<link rel="chapter" href="zend.gdata.html" title="Chapter 18. Zend_Gdata">
<link rel="chapter" href="zend.http.html" title="Chapter 19. Zend_Http">
<link rel="chapter" href="zend.infocard.html" title="Chapter 20. Zend_InfoCard">
<link rel="chapter" href="zend.json.html" title="Chapter 21. Zend_Json">
<link rel="chapter" href="zend.layout.html" title="Chapter 22. Zend_Layout">
<link rel="chapter" href="zend.ldap.html" title="Chapter 23. Zend_Ldap">
<link rel="chapter" href="zend.loader.html" title="Chapter 24. Zend_Loader">
<link rel="chapter" href="zend.locale.html" title="Chapter 25. Zend_Locale">
<link rel="chapter" href="zend.log.html" title="Chapter 26. Zend_Log">
<link rel="chapter" href="zend.mail.html" title="Chapter 27. Zend_Mail">
<link rel="chapter" href="zend.measure.html" title="Chapter 28. Zend_Measure">
<link rel="chapter" href="zend.memory.html" title="Chapter 29. Zend_Memory">
<link rel="chapter" href="zend.mime.html" title="Chapter 30. Zend_Mime">
<link rel="chapter" href="zend.openid.html" title="Chapter 31. Zend_OpenId">
<link rel="chapter" href="zend.paginator.html" title="Chapter 32. Zend_Paginator">
<link rel="chapter" href="zend.pdf.html" title="Chapter 33. Zend_Pdf">
<link rel="chapter" href="zend.registry.html" title="Chapter 34. Zend_Registry">
<link rel="chapter" href="zend.rest.html" title="Chapter 35. Zend_Rest">
<link rel="chapter" href="zend.search.lucene.html" title="Chapter 36. Zend_Search_Lucene">
<link rel="chapter" href="zend.server.html" title="Chapter 37. Zend_Server">
<link rel="chapter" href="zend.service.html" title="Chapter 38. Zend_Service">
<link rel="chapter" href="zend.session.html" title="Chapter 39. Zend_Session">
<link rel="chapter" href="zend.soap.html" title="Chapter 40. Zend_Soap">
<link rel="chapter" href="zend.test.html" title="Chapter 41. Zend_Test">
<link rel="chapter" href="zend.text.html" title="Chapter 42. Zend_Text">
<link rel="chapter" href="zend.timesync.html" title="Chapter 43. Zend_TimeSync">
<link rel="chapter" href="zend.translate.html" title="Chapter 44. Zend_Translate">
<link rel="chapter" href="zend.uri.html" title="Chapter 45. Zend_Uri">
<link rel="chapter" href="zend.validate.html" title="Chapter 46. Zend_Validate">
<link rel="chapter" href="zend.version.html" title="Chapter 47. Zend_Version">
<link rel="chapter" href="zend.view.html" title="Chapter 48. Zend_View">
<link rel="chapter" href="zend.xmlrpc.html" title="Chapter 49. Zend_XmlRpc">
<link rel="appendix" href="requirements.html" title="Appendix A. Zend Framework Requirements">
<link rel="appendix" href="coding-standard.html" title="Appendix B. Zend Framework Coding Standard for PHP">
<link rel="appendix" href="copyrights.html" title="Appendix C. Copyright Information">
<link rel="index" href="the.index.html" title="Index">
<link rel="subsection" href="zend.locale.date.datesandtimes.html#zend.locale.date.normalize" title="25.4.1. Normalizing Dates and Times">
<link rel="subsection" href="zend.locale.date.datesandtimes.html#zend.locale.date.test" title="25.4.2. Testing Dates">
<link rel="subsection" href="zend.locale.date.datesandtimes.html#zend.locale.time.normalizing" title="25.4.3. Normalizing a Time">
<link rel="subsection" href="zend.locale.date.datesandtimes.html#zend.locale.time.test" title="25.4.4. Testing Times">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader"><table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">25.4. Working with Dates and Times</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="zend.locale.parsing.html">Prev</a> </td>
<th width="60%" align="center">Chapter 25. Zend_Locale</th>
<td width="20%" align="right"> <a accesskey="n" href="zend.locale.languages.html">Next</a>
</td>
</tr>
</table></div>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="zend.locale.date.datesandtimes"></a>25.4. Working with Dates and Times</h2></div></div></div>
<p>
        <code class="code">Zend_Locale_Format</code> provides several methods for working with dates and times to help convert and
        normalize between different formats for different locales. Use <code class="code">Zend_Date</code> for manipulating dates,
        and working with date strings that already conform to
        <a href="zend.date.constants.html" title="9.6. Constants for General Date Functions">one of the many internationally recognized standard formats, or one of the localized date formats supported by <code class="code">Zend_Date</code>
        </a>
        . Using an existing, pre-defined format offers advantages, including the use of well-tested code, and the
        assurance of some degree of portability and interoperability (depending on the standard used). The examples
        below do not follow these recommendations, since using non-standard date formats would needlessly increase the
        difficulty of understanding these examples.
    </p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.locale.date.normalize"></a>25.4.1. Normalizing Dates and Times</h3></div></div></div>
<p>
            The <code class="code">getDate()</code> method parses strings containing dates in localized formats. The results are
            returned in a structured array, with well-defined keys for each part of the date. In addition, the array
            will contain a key 'date_format' showing the format string used to parse the input date string. Since a
            localized date string may not contain all parts of a date/time, the key-value pairs are optional. For
            example, if only the year, month, and day is given, then all time values are supressed from the returned
            array, and vice-versa if only hour, minute, and second were given as input. If no date or time can be found
            within the given input, an exception will be thrown.
        </p>
<p>
            If <code class="code">setOption(array('fix_date' =&gt; true))</code> is set the <code class="code">getDate()</code> method adds a key 'fixed'
            with a whole number value indicating if the input date string required "fixing" by rearranging the day,
            month, or year in the input to fit the format used.
        </p>
<div class="table">
<a name="zend.locale.date.normalize.table-1"></a><p class="title"><b>Table 25.6. Key values for getDate() with option 'fix_date'</b></p>
<div class="table-contents"><table summary="Key values for getDate() with option 'fix_date'" border="1">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>value</th>
<th>meaning</th>
<td class="auto-generated"> </td>
<td class="auto-generated"> </td>
</tr></thead>
<tbody>
<tr>
<td>0</td>
<td>nothing to fix</td>
<td class="auto-generated"> </td>
<td class="auto-generated"> </td>
</tr>
<tr>
<td>1</td>
<td>fixed false month</td>
<td class="auto-generated"> </td>
<td class="auto-generated"> </td>
</tr>
<tr>
<td>2</td>
<td>swapped day and year</td>
<td class="auto-generated"> </td>
<td class="auto-generated"> </td>
</tr>
<tr>
<td>3</td>
<td>swapped month and year</td>
<td class="auto-generated"> </td>
<td class="auto-generated"> </td>
</tr>
<tr>
<td>4</td>
<td>swapped month and day</td>
<td class="auto-generated"> </td>
<td class="auto-generated"> </td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
            For those needing to specify explicitly the format of the date string, the following format token specifiers
            are supported. If an invalid format specifier is used, such as the PHP 'i' specifier when in ISO format
            mode, then an error will be thrown by the methods in Zend_Locale_Format that support user-defined formats.
        </p>
<p>
            These specifiers (below) are a small subset of the full "ISO" set supported by Zend_Date's
            <code class="code">toString()</code>. If you need to use PHP <code class="code">date()</code> compatible format specifiers, then first
            call <code class="code">setOptions(array('format_type' =&gt; 'php'))</code>. And if you want to convert only one special format
            string from PHP <code class="code">date()</code> compatible format to "ISO" format use <code class="code">convertPhpToIsoFormat()</code>.
            Currently, the only practical difference relates to the specifier for minutes ('m' using the ISO default,
            and 'i' using the PHP date format).
        </p>
<div class="table">
<a name="zend.locale.date.normalize.table-2"></a><p class="title"><b>Table 25.7. Return values</b></p>
<div class="table-contents"><table summary="Return values" border="1">
<colgroup>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>getDate() format character</th>
<th>Array key</th>
<th>Returned value</th>
<th>Minimum</th>
<th>Maximum</th>
</tr></thead>
<tbody>
<tr>
<td>d</td>
<td>day</td>
<td>integer</td>
<td>1</td>
<td>31</td>
</tr>
<tr>
<td>M</td>
<td>month</td>
<td>integer</td>
<td>1</td>
<td>12</td>
</tr>
<tr>
<td>y</td>
<td>year</td>
<td>integer</td>
<td>no limit</td>
<td>PHP integer's maximum</td>
</tr>
<tr>
<td>h</td>
<td>hour</td>
<td>integer</td>
<td>0</td>
<td>PHP integer's maximum</td>
</tr>
<tr>
<td>m</td>
<td>minute</td>
<td>integer</td>
<td>0</td>
<td>PHP integer's maximum</td>
</tr>
<tr>
<td>s</td>
<td>second</td>
<td>integer</td>
<td>0</td>
<td>PHP integer's maximum</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><div class="example">
<a name="zend.locale.date.normalize.example-1"></a><p class="title"><b>Example 25.38. Normalizing a date</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Locale.php';

$dateString = Zend_Locale_Format::getDate('13.04.2006', array('date_format' =&gt; 'dd.MM.yyyy'));
$dateObject = Zend_Date('13.04.2006', array('date_format' =&gt; 'dd.MM.yyyy')); // creates a Zend_Date object for this date

print_r($dateString); // outputs:

Array
(
    [format] =&gt; dd.MM.yyyy
    [day] =&gt; 13
    [month] =&gt; 4
    [year] =&gt; 2006
)

// alternatively, some types of problems with input data can be automatically corrected
$date2 = Zend_Locale_Format::getDate('04.13.2006', array('date_format' =&gt; 'dd.MM.yyyy', 'fix_date' =&gt; true));

print_r($date); // outputs:

Array
(
    [format] =&gt; dd.MM.yyyy
    [day] =&gt; 13
    [month] =&gt; 4
    [year] =&gt; 2006
    [fixed] =&gt; 4
)
            </pre></div>
</div>
<br class="example-break"><p>
            Since <code class="code">getDate()</code> is "locale-aware", specifying the <code class="code">$locale</code> is sufficient for date
            strings adhering to that locale's format. The option '<code class="code">fix_date</code>' uses simple tests to
            determine if the day or month is not valid, and then applies heuristics to try and correct any detected
            problems. Note the use of '<code class="code">Zend_Locale_Format::STANDARD</code>' as the value for
            '<code class="code">date_format</code>' to prevent the use of a class-wide default date format set using
            <code class="code">setOptions()</code>. This forces getDate to use the default date format for <code class="code">$locale</code>.
        </p>
<div class="example">
<a name="zend.locale.date.normalize.example-2"></a><p class="title"><b>Example 25.39. Normalizing a date by locale</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
$date = Zend_Locale_Format::getDate('13.04.2006', array('date_format' =&gt; Zend_Locale_Format::STANDARD, 'locale' =&gt; $locale));

print_r ($date);
            </pre></div>
</div>
<br class="example-break"><p>
            A complete date and time is returned when the input contains both a date and time in the expected format.
        </p>
<div class="example">
<a name="zend.locale.date.normalize.example-3"></a><p class="title"><b>Example 25.40. Normalizing a date with time</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
$date = Zend_Locale_Format::getDate('13.04.2005 22:14:55', array('date_format' =&gt; Zend_Locale_Format::STANDARD, 'locale' =&gt; $locale);

print_r ($date);
            </pre></div>
</div>
<br class="example-break"><p>
            If a specific format is desired, specify the <code class="code">$format</code> argument, without giving a
            <code class="code">$locale</code>. Only single-letter codes (H, m, s, y, M, d), and MMMM and EEEE are supported in the
            <code class="code">$format</code>.
        </p>
<div class="example">
<a name="zend.locale.date.normalize.example-4"></a><p class="title"><b>Example 25.41. Normalizing a userdefined date</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Locale.php';

$date = Zend_Locale_Format::getDate('13200504T551422', array('date_format' =&gt; 'ddyyyyMM ssmmHH');

print_r ($date);
            </pre></div>
</div>
<br class="example-break"><p>
            The format can include the following signs :
        </p>
<div class="table">
<a name="zend.locale.date.normalize.table-3"></a><p class="title"><b>Table 25.8. Format definition</b></p>
<div class="table-contents"><table summary="Format definition" border="1">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>Format Letter</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td>d or dd</td>
<td>1 or 2 digit day</td>
</tr>
<tr>
<td>M or MM</td>
<td>1 or 2 digit month</td>
</tr>
<tr>
<td>y or yy</td>
<td>1 or 2 digit year</td>
</tr>
<tr>
<td>yyyy</td>
<td>4 digit year</td>
</tr>
<tr>
<td>h</td>
<td>1 or 2 digit hour</td>
</tr>
<tr>
<td>m</td>
<td>1 or 2 digit minute</td>
</tr>
<tr>
<td>s</td>
<td>1 or 2 digit second</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
            Examples for proper formats are
        </p>
<div class="table">
<a name="zend.locale.date.normalize.table-4"></a><p class="title"><b>Table 25.9. Example formats</b></p>
<div class="table-contents"><table summary="Example formats" border="1">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>Formats</th>
<th>Input</th>
<th>Output</th>
</tr></thead>
<tbody>
<tr>
<td>dd.MM.yy</td>
<td>1.4.6</td>
<td>['day'] =&gt; 1,
                                   ['month'] =&gt; 4,
                                   ['year'] =&gt; 6</td>
</tr>
<tr>
<td>dd.MM.yy</td>
<td>01.04.2006</td>
<td>['day'] =&gt; 1, ['month'] =&gt; 4, ['year'] =&gt; 2006</td>
</tr>
<tr>
<td>yyyyMMdd</td>
<td>1.4.6</td>
<td>['day'] =&gt; 6, ['month'] =&gt; 4, ['year'] =&gt; 1</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><div class="note"><table border="0" summary="Note: Database date format">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td>
<th align="left">Database date format</th>
</tr>
<tr><td align="left" valign="top"><p>
                To parse a database date value (f.e. MySql or MsSql), use Zend_Date's ISO_8601 format instead of
                getDate().
            </p></td></tr>
</table></div>
<p>
            The option '<code class="code">fix_date</code>' uses simple tests to determine if the day or month is not
            valid, and then applies heuristics to try and correct any detected problems. <code class="code">getDate()</code>
            automatically detects and corrects some kinds of problems with input, such as misplacing the year:
        </p>
<div class="example">
<a name="zend.locale.date.normalize.example-5"></a><p class="title"><b>Example 25.42. Automatic correction of input dates</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Locale.php';

$date = Zend_Locale_Format::getDate('41.10.20', array('date_format' =&gt; 'ddMMyy', 'fix_date' =&gt; true));

// instead of 41 for the day, the 41 will be returned as year value
print_r ($date);
            </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.locale.date.test"></a>25.4.2. Testing Dates</h3></div></div></div>
<p>
            Use <code class="code">checkDateFormat($inputString, array('date_format' =&gt; $format, $locale))</code> to check if a given string
            contains all expected date parts. The <code class="code">checkDateFormat()</code> method uses <code class="code">getDate()</code>, but without
            the option <code class="code">'fixdate'</code> to avoid returning true when the input fails to conform to the date
            format. If errors are detected in the input, such as swapped values for months and days, the
            option <code class="code">'fixdate'</code> method will apply heuristics to "correct" dates before determining their
            validity.
        </p>
<div class="example">
<a name="zend.locale.date.test.example-1"></a><p class="title"><b>Example 25.43. Date testing</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
// using the default date format for 'de_AT', is this a valid date?
if (Zend_Locale_Format::checkDateFormat('13.Apr.2006', array('date_format' =&gt; Zend_Locale_Format::STANDARD, $locale)) {
    print "date";
} else {
    print "not a date";
}
            </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.locale.time.normalizing"></a>25.4.3. Normalizing a Time</h3></div></div></div>
<p>
            Normally, a time will be returned with a date, if the input contains both. If the proper format is not
            known, but the locale relevant to the user input is known, then <code class="code">getTime()</code> should be used,
            because it uses the default time format for the selected locale.
        </p>
<div class="example">
<a name="zend.locale.time.normalizing.example-1"></a><p class="title"><b>Example 25.44. Normalize an unknown time</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::getTime('13:44:42', array('date_format' =&gt; Zend_Locale_Format::STANDARD, 'locale' =&gt; $locale)) {
    print "time";
} else {
    print "not a time";
}
            </pre></div>
</div>
<br class="example-break">
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="zend.locale.time.test"></a>25.4.4. Testing Times</h3></div></div></div>
<p>
            Use <code class="code">checkDateFormat()</code> to check if a given string contains a proper time.
            The usage is exact the same as with checking Dates, only <code class="code">date_format</code>
            should contain the parts which you expect to have.
        </p>
<div class="example">
<a name="zend.locale.time.test.example-1"></a><p class="title"><b>Example 25.45. Testing a time</b></p>
<div class="example-contents"><pre class="programlisting">&lt;?php
require_once 'Zend/Locale.php';

$locale = new Zend_Locale('de_AT');
if (Zend_Locale_Format::checkDateFormat('13:44:42', array('date_format' =&gt; 'HH:mm:ss', 'locale' =&gt; $locale)) {
    print "time";
} else {
    print "not a time";
}
            </pre></div>
</div>
<br class="example-break">
</div>
</div>
<div class="navfooter"><table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="zend.locale.parsing.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="zend.locale.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="zend.locale.languages.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">25.3. Normalization and Localization </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> 25.5. Supported Languages for Locales</td>
</tr>
</table></div>
<div class="revinfo"></div>
</body>
</html>
